﻿using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.ShowContent
{

    public partial class View : BasePage
    {
        protected static string style = "demoContentBody_sh";
        protected static string pagelocationname = string.Empty;
        protected static string pageLocaComments = string.Empty;
        protected static string proLabid = string.Empty;
        protected static string proLocationid = string.Empty;
        protected static string locationUrl = string.Empty;
        public int LocationId
        {
            get
            {
                return (Id == 0 ? 1 : Id);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                string labId = GetQueryString("labid");
                string productId = GetQueryString("productid");
                if (string.IsNullOrEmpty(productId))
                {
                    if (string.IsNullOrEmpty(labId))
                    {
                        if (LocationPresent.GetById(LocationId) == null)
                            this.divmain.Visible = false;
                        else
                            LocationBind(LocationId);
                    }
                    else
                    {
                        int labid = int.Parse(labId);
                        if (LabPresent.GetById(labid) == null)
                            this.divmain.Visible = false;
                        else
                            LabBind(labid);
                    }
                }
                else
                {
                    int productid = int.Parse(productId);
                    if (ProductPresent.GetById(productid) == null)
                        this.divmain.Visible = false;
                    else
                        ProductBind(productid);
                }
            }
        }

        public override void InitControlResource()
        {
            this.lblDemotype.Text = NameResource.Product_DemoTpye;
            this.lblDemocontent.Text = NameResource.Product_DemoContent;
            this.lblDemostep.Text = NameResource.Product_DemoStep;
            this.lblKeycomments.Text = NameResource.Product_KeyComments;
            this.lblEquipment.Text = NameResource.Product_Equipment;
            this.lblLanguage.Text = NameResource.Language;
        }

        protected void LocationBind(int locationid)
        {
            if (locationid == 1)
                style = "demoContentBody_sh";
            if (locationid == 2)
                style = "demoContentBody_bj";
            if (locationid == 3)
                style = "demoContentBody_nj";
            if (locationid == 4)
                style = "demoContentBody_cd";
            if (locationid == 5)
                style = "demoContentBody_qd";

            this.divProductLeft.Visible = false;
            this.divProductRight.Visible = false;
            this.divdemoContentBodyLeft.Visible = true;
            this.divdemoContentBodyRight.Visible = true;

            blistLab.DataSource = LabPresent.GetList(g => g.LocationId == locationid);
            blistLab.DataValueField = "LabId";
            blistLab.DataTextField = "Name";
            blistLab.DataBind();

            var lab = LabPresent.GetByCondition(g => g.LocationId == locationid);
            if (lab != null)
            {
                proLabid = lab.LabId.ToString();
                proLocationid = lab.LocationId.ToString();
                rptProduct.DataSource = ProductPresent.GetList(g => g.LabId == lab.LabId);
                rptProduct.DataBind();
            }
            else
            {
                rptProduct.Visible = false;
            }
            demoContentBodyRightBind(locationid);
        }

        protected void LabBind(int labid)
        {
            this.divProductLeft.Visible = false;
            this.divProductRight.Visible = false;
            this.divdemoContentBodyLeft.Visible = true;
            this.divdemoContentBodyRight.Visible = true;
            var locationId = (Id == 0 ? 1 : Id);
            LocationBind(locationId);
            var lab = LabPresent.GetById(labid);
            if (lab != null)
            {
                proLabid = labid.ToString();
                proLocationid = lab.LocationId.ToString();
            }
            rptProduct.DataSource = ProductPresent.GetList(g => g.LabId == labid);
            rptProduct.DataBind();
            ListItem labli = blistLab.Items.FindByValue(labid.ToString());
            string backcolor = "background:#e9f8ff;width:394px;";
            if (locationId == 1)
                backcolor = "background:#e9f8ff;width:394px;";
            if (locationId == 2)
                backcolor = "background:#ffebbe;width:394px;";
            if (locationId == 3)
                backcolor = "background:#fff6d2;width:394px;";
            if (locationId == 4)
                backcolor = "background:#ffdada;width:394px;";
            if (locationId == 5)
                backcolor = "background:#f4ffea;width:394px;";
            labli.Attributes.Add("style", backcolor);
        }

        protected void ProductBind(int productid)
        {
            this.divdemoContentBodyLeft.Visible = false;
            this.divdemoContentBodyRight.Visible = false;
            this.divProductRight.Visible = true;
            this.divProductLeft.Visible = true;
            var productres = ProductPresent.GetById(productid);
            var productpicres = ProductPicPresent.GetList(g => g.ProductId == productid);
            string name = string.Empty;
            string demotype = string.Empty;
            string democontent = string.Empty;
            string demostep = string.Empty;
            string keycomments = string.Empty;
            string equipment = string.Empty;
            if (productres != null)
            {
                if (Language == Utility.Language.CN)
                {
                    name = productres.Name_CN;
                    demotype = productres.DemoType_CN;
                    democontent = productres.DemoContent_CN;
                    demostep = productres.DemoStep_CN;
                    keycomments = productres.KeyComments_CN;
                    equipment = productres.Equipment_CN;
                }
                else if (Language == Utility.Language.EN)
                {
                    name = productres.Name_EN;
                    demotype = productres.DemoType_EN;
                    democontent = productres.DemoContent_EN;
                    demostep = productres.DemoStep_EN;
                    keycomments = productres.KeyComment_EN;
                    equipment = productres.Equipment_EN;
                }
            }
            this.lblProductname.Text = name;
            this.literDemotype.Text = Server.HtmlDecode(demotype);
            this.literDemocontent.Text = democontent == null ? democontent : Server.HtmlDecode(democontent).Replace("\r\n", "<br/>");
            this.literDemostep.Text = demostep == null ? demostep : Server.HtmlDecode(demostep).Replace("\r\n", "<br/>");
            this.literKeycomments.Text = keycomments == null ? keycomments : Server.HtmlDecode(keycomments).Replace("\r\n", "<br/>");
            this.literEquipment.Text = equipment == null ? equipment : Server.HtmlDecode(equipment).Replace("\r\n", "<br/>");
            this.literLanguage.Text = string.Empty;
            rptProductpic.DataSource = productpicres;
            rptProductpic.DataBind();
            SetControlVisible(divProductname, !string.IsNullOrWhiteSpace(name));
            SetControlVisible(lblDemotype, !string.IsNullOrWhiteSpace(demotype));
            SetControlVisible(literDemotype, !string.IsNullOrWhiteSpace(demotype));
            SetControlVisible(lblDemocontent, !string.IsNullOrWhiteSpace(democontent));
            SetControlVisible(literDemocontent, !string.IsNullOrWhiteSpace(democontent));
            SetControlVisible(lblDemostep, !string.IsNullOrWhiteSpace(demostep));
            SetControlVisible(literDemostep, !string.IsNullOrWhiteSpace(demostep));
            SetControlVisible(lblKeycomments, !string.IsNullOrWhiteSpace(keycomments));
            SetControlVisible(literKeycomments, !string.IsNullOrWhiteSpace(keycomments));
            SetControlVisible(lblEquipment, !string.IsNullOrWhiteSpace(equipment));
            SetControlVisible(literEquipment, !string.IsNullOrWhiteSpace(equipment));
            if (productres != null)
            {
                if (!(Boolean)productres.ChineseIntroduce && !(Boolean)productres.EnglishIntroduce)
                {
                    this.lblLanguage.Visible = false;
                    this.literLanguage.Visible = false;
                }
                else
                {
                    this.lblLanguage.Visible = true;
                    this.literLanguage.Visible = true;
                    if ((Boolean)productres.ChineseIntroduce && !(Boolean)productres.EnglishIntroduce)
                        this.literLanguage.Text = NameResource.Language_CN_Label;
                    else if (!(Boolean)productres.ChineseIntroduce && (Boolean)productres.EnglishIntroduce)
                        this.literLanguage.Text = NameResource.Language_EN_Label;
                    else if ((Boolean)productres.ChineseIntroduce && (Boolean)productres.EnglishIntroduce)
                        this.literLanguage.Text = NameResource.Language_CN_Label + "," + NameResource.Language_EN_Label;
                }
            }
        }

        protected void blistLab_Click(object sender, BulletedListEventArgs e)
        {
            ListItem labli = blistLab.Items[e.Index];
            int labid = int.Parse(labli.Value);
            Response.Redirect("view.aspx?Id=" + Id + "&labid=" + labid);
        }

        protected void lvProduct_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Model.Product product = e.Item.DataItem as Model.Product;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var lblName = e.Item.FindControl("lblName") as Literal;
                if (lblName != null)
                {
                    lblName.Text = Language == Language.CN ? product.Name_CN : product.Name_EN;
                }
            }
        }


        private void SetControlVisible(Control control, bool b)
        {
            control.Visible = b;
        }

        private void demoContentBodyRightBind(int i)
        {
            string shlocationname = Resources.NameResource.Location_Shanghai;
            string bjlocationname = Resources.NameResource.Location_Beijing;
            string njlocationname = Resources.NameResource.Location_Nanjing;
            string cdlocationname = Resources.NameResource.Location_Chengdu;
            string qdlocationname = Resources.NameResource.Location_Qingdao;
            string shsrc1 = "../Content/images/dc001.png";
            string shsrc2 = "../Content/images/dc002.png";
            string shsrc3 = "../Content/images/dc003.png";
            string bjsrc1 = "../Content/images/dc004.png";
            string bjsrc2 = "../Content/images/dc005.png";
            string bjsrc3 = "../Content/images/dc006.png";
            string njsrc1 = "../Content/images/dc007.png";
            string njsrc2 = "../Content/images/dc008.png";
            string njsrc3 = "../Content/images/dc009.png";
            string cdsrc1 = "../Content/images/dc010.png";
            string cdsrc2 = "../Content/images/dc011.png";
            string cdsrc3 = "../Content/images/dc012.png";
            string qdsrc1 = "../Content/images/dc013.png";
            string qdsrc2 = "../Content/images/dc014.png";
            string qdsrc3 = "../Content/images/dc015.png";
            string shlocationComment = Resources.NameResource.Location_Shanghai_ShowComment;
            string bjlocationComment = Resources.NameResource.Location_Beijing_ShowComment;
            string njlocationComment = Resources.NameResource.Location_Nanjing_ShowComment;
            string cdlocationComment = Resources.NameResource.Location_Chengdu_ShowComment;
            string qdlocationComment = Resources.NameResource.Location_Qingdao_ShowComment;
            switch (i)
            {
                case 1:
                    SetImgSrc(shlocationname, shsrc1, shsrc2, shsrc3, shlocationComment);
                    locationUrl = "/FrontPages/LocationSHIntroduce.aspx";
                    break;
                case 2:
                    SetImgSrc(bjlocationname, bjsrc1, bjsrc2, bjsrc3, bjlocationComment);
                    locationUrl = "/FrontPages/LocationBJIntroduce.aspx";
                    break;
                case 3:
                    SetImgSrc(njlocationname, njsrc1, njsrc2, njsrc3, njlocationComment);
                    locationUrl = "/FrontPages/LocationNJIntroduce.aspx";
                    break;
                case 4:
                    SetImgSrc(cdlocationname, cdsrc1, cdsrc2, cdsrc3, cdlocationComment);
                    locationUrl = "/FrontPages/LocationCDIntroduce.aspx";
                    break;
                case 5:
                    SetImgSrc(qdlocationname, qdsrc1, qdsrc2, qdsrc3, qdlocationComment);
                    locationUrl = "/FrontPages/LocationQDIntroduce.aspx";
                    break;
                default:
                    break;
            }
        }
        private void SetImgSrc(string locationname, string src1, string src2, string src3, string locationComment)
        {
            pagelocationname = locationname;
            imgProductpic1.Src = src1;
            imgProductpic2.Src = src2;
            imgProductpic3.Src = src3;
            pageLocaComments = locationComment;
        }

        protected void lbtnPrevious_Click(object sender, EventArgs e)
        {
            var locationId = (Id == 0 ? 1 : Id);
            string labId = GetQueryString("labid");
            string productId = GetQueryString("productid");
            if (string.IsNullOrEmpty(productId))
            {
                int labid;
                if (string.IsNullOrEmpty(labId))
                {
                    var lab = LabPresent.GetList(g => g.LocationId == locationId).FirstOrDefault();
                    if (lab != null)
                        labid = lab.LabId;
                    else
                        return;
                }
                else
                {
                    labid = int.Parse(labId);
                }
                var location = LabPresent.GetById(labid);
                if (location != null)
                {
                    var locationid = location.LocationId;
                    var lab = LabPresent.GetList(g => g.LocationId == locationid && g.LabId < labid).OrderBy(g => g.LabId).LastOrDefault();
                    if (lab == null)
                        lab = LabPresent.GetList(g => g.LocationId == locationid).OrderBy(g => g.LabId).LastOrDefault();
                    Response.Redirect("view.aspx?Id=" + locationId + "&labid=" + lab.LabId);
                }
                else
                    return;

            }
            else
            {
                int nowproid = int.Parse(productId);
                var pro = ProductPresent.GetById(nowproid);
                if (pro != null)
                {
                    var labid = pro.LabId;
                    var product = ProductPresent.GetList(g => g.LabId == labid && g.ProductId < nowproid).OrderBy(g => g.ProductId).LastOrDefault();
                    if (product == null)
                        Response.Redirect("view.aspx?Id=" + locationId + "&labid=" + labid);
                    else
                        Response.Redirect("view.aspx?Id=" + locationId + "&labid=" + labid + "&productid=" + product.ProductId);
                }
                else
                    return;
            }
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            var locationId = (Id == 0 ? 1 : Id);
            string labId = GetQueryString("labid");
            string productId = GetQueryString("productid");
            if (string.IsNullOrEmpty(productId))
            {
                int labid;
                if (string.IsNullOrEmpty(labId))
                {
                    var lab = LabPresent.GetList(g => g.LocationId == locationId).FirstOrDefault();
                    if (lab != null)
                        labid = lab.LabId;
                    else
                        return;
                }
                else
                {
                    labid = int.Parse(labId);
                }
                var location = LabPresent.GetById(labid);
                if (location != null)
                {
                    var locationid = location.LocationId;
                    var lab = LabPresent.GetList(g => g.LocationId == locationid && g.LabId > labid).OrderBy(g => g.LabId).FirstOrDefault();
                    if (lab == null)
                        lab = LabPresent.GetList(g => g.LocationId == locationid).OrderBy(g => g.LabId).FirstOrDefault();
                    Response.Redirect("view.aspx?Id=" + locationId + "&labid=" + lab.LabId);
                }
                else
                    return;

            }
            else
            {
                int nowproid = int.Parse(productId);
                var pro = ProductPresent.GetById(nowproid);
                if (pro != null)
                {
                    var labid = pro.LabId;
                    var product = ProductPresent.GetList(g => g.LabId == labid && g.ProductId > nowproid).OrderBy(g => g.ProductId).FirstOrDefault();
                    if (product == null)
                        Response.Redirect("view.aspx?Id=" + locationId + "&labid=" + labid);
                    else 
                        Response.Redirect("view.aspx?Id=" + locationId + "&labid=" + labid + "&productid=" + product.ProductId);
                }
                else
                    return;
            }
        }

    }
}